package com.example.listener.page

import android.media.AudioManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.example.listener.databinding.FragmentVolumeBinding

/**
 *
 * @author: xct
 * create on: 2022/8/30 10:54
 *
 */
class VolumeFragment : Fragment() {


    private lateinit var viewBinding: FragmentVolumeBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        return FragmentVolumeBinding.inflate(inflater, container, false)
            .also { viewBinding = it }
            .root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val context = view.context

        val audioManager = ContextCompat.getSystemService(context, AudioManager::class.java)
        viewBinding.tvInfo.apply {
            append("alarm:")
            append(getInfo(audioManager,AudioManager.STREAM_ALARM))
            append("\n")
            append("notification:")
            append(getInfo(audioManager,AudioManager.STREAM_NOTIFICATION))
            append("\n")
            append("music:")
            append(getInfo(audioManager,AudioManager.STREAM_MUSIC))
        }

        viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                super.onCreate(owner)

            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
//                volumeListener.unregister()
                viewLifecycleOwner.lifecycle.removeObserver(this)
            }
        })
    }

    fun getInfo(audioManager: AudioManager?,streamType:Int):String{
        return "max:${audioManager?.getStreamMaxVolume(streamType)}"
    }

}